package com.xiaodeng.xdblogspringboot.controller;


import com.github.pagehelper.PageInfo;
import com.xiaodeng.xdblogspringboot.pojo.Comments;
import com.xiaodeng.xdblogspringboot.pojo.DTO.CommentCreateDTO;
import com.xiaodeng.xdblogspringboot.pojo.DTO.CommentDTO;
import com.xiaodeng.xdblogspringboot.pojo.Result;
import com.xiaodeng.xdblogspringboot.pojo.VO.CommentVO;
import com.xiaodeng.xdblogspringboot.pojo.VO.LikesVO;
import com.xiaodeng.xdblogspringboot.service.CommentsService;
import com.xiaodeng.xdblogspringboot.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @PostMapping("/update")
    public Result updateLike(CommentDTO dto) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        dto.setUserId(Long.valueOf(userId));
        commentsService.updateLike(dto);
        return Result.success("点赞量更新成功");
    }

    @PostMapping("/add")
    public Result addComment(@RequestBody CommentCreateDTO dto) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        dto.setUserId(Long.valueOf(userId));
        Comments comment = commentsService.createComment(dto);
        return Result.success("评论添加成功");
    }

    @GetMapping("/list")
    public Result getCommentsByArticleId(@RequestParam Long articleId) {
        List<CommentVO> comments = commentsService.getCommentsByArticleId(articleId);
        return Result.success("查询成功", comments);
    }

    @PostMapping("/delete")
    public Result deleteComment(@RequestParam Long commentId) {
        commentsService.deleteComment(commentId);
        return Result.success("评论已删除");
    }

    /**
     * 查询目标ID的有效点赞记录（带用户信息）
     */
    @GetMapping("/listLikes")
    public Result<List<LikesVO>> getLikesList(
            @RequestParam("targetId") Long targetId,
            @RequestParam(required = false) Short likeType) {
        List<LikesVO> likesList = commentsService.getValidLikes(targetId, likeType);
        return Result.success(likesList);
    }
    /**
     * 分页查询所有评论，支持多条违规词搜索
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param sensitiveWord 逗号分隔的违规词字符串
     * @return 评论分页信息
     */
    @GetMapping("/all")
    public Result<PageInfo<CommentVO>> getAllCommentsWithoutHierarchy(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String sensitiveWord) {
        PageInfo<CommentVO> pageInfo = commentsService.getAllCommentsWithoutHierarchy(pageNum, pageSize, sensitiveWord);
        return Result.success("查询成功", pageInfo);
    }
    @PostMapping("/batchDelete")
    public Result batchDeleteComments(@RequestBody List<Long> commentIds) {
        commentsService.batchDeleteComments(commentIds);
        return Result.success("评论批量删除成功");
    }
}